package com.coffee.service.impl;

import com.coffee.context.BaseContext;
import com.coffee.dto.ShoppingCartDTO;
import com.coffee.entity.Dish;
import com.coffee.entity.Setmeal;
import com.coffee.entity.ShoppingCart;
import com.coffee.mapper.DishMapper;
import com.coffee.mapper.SetmealMapper;
import com.coffee.mapper.ShoppingCartMapper;
import com.coffee.result.Result;
import com.coffee.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断商品是否已经存在
        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        if (list!=null&&list.size()>0){
            //若存在则数量加一
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            //更新数据库
            shoppingCartMapper.uppdateNumberById(cart);
        }else {
            //若不存在则插入数据
            if (shoppingCartDTO.getDishId()!=null){
                //菜品
                Dish dish=dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else{
                //套餐
                Setmeal setmeal=setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list!=null&&list.size()>0){
            //如果当前商品在购物车中的数量>1，修改商品数量
            Integer number = list.get(0).getNumber();
            if(number >1){
                list.get(0).setNumber(number-1);
                shoppingCartMapper.uppdateNumberById(list.get(0));
            }else{
                //如果商品数量为1，直接删除
                shoppingCartMapper.deleteById(list.get(0).getId());
            }
        }


    }


}
